<?php
/**
 * ActivityModel.class.php
 * 主题活动模型
 * 
 * @author  王中艺 <wangzy_smile@qq.com>
 * @date    2017-05-13
 */

namespace Wechat\Model;

class ActivityModel extends BaseModel {
    //定义数据表明
    protected $trueTableName    = 't_activity';

    const   LEAVE_MSG_TYPE = 2;
    
    /**
     * 获取最新主题
     * @param   $level 主体等级
     * @return  $activityInfo
     */
    function getNewItem($level){
        $activityInfo   = $this->where('status = 1 and level = %d', $level)
            ->order('id desc')
            ->cache(true)
            ->find();
        $activityInfo   = $this->_analyField('activity', $activityInfo);

        $knownList  = M('t_activity_known', null)
            ->where('activity_id = %d and status != -1', $activityInfo['id'])
            ->order('id desc')
            ->cache(true)
            ->select();
        $activityInfo['known_list']     = $this->_analyField('activityknown', $knownList, true);

        return $activityInfo;
    }

    /**
     * 获取主题列表
     * @param   $level
     * @param   $page
     * @return  $list
     */
    function getList($level, $page = 1){
        $list   = $this->where('status = 1 and level = %d', $level)
            ->order('id desc')
            ->cache(true)
            ->page($page, C('PAGE_COUNT'))
            ->field('id, title, banner, create_time, update_time, status')
            ->select();

        $list   = $this->_analyField('activity', $list, true);
        return $list;
    }

    /**
     * 获取指定主题
     * @param   $activityId
     * @return  $activityInfo
     */
    function getItem($activityId){
        $activityInfo   = $this->where('id = %d and status != -1', $activityId)->find();
        if(!$activityInfo){
            $this->errMsg   = '指定主题未找到!';
            return false;
        }
        $activityInfo   = $this->_analyField('activity', $activityInfo);

        $knownList  = M('t_activity_known', null)
            ->where('activity_id = %d and status != -1', $activityId)
            ->order('id desc')
            ->cache(true)
            ->select();
        $activityInfo['known_list']     = $this->_analyField('activityknown', $knownList, true);
        
        return $activityInfo;
    }

    /**
     * 添加活动照片
     * @param   $activityId
     * @param   $userId
     * @param   $childPics  宝宝拍摄
     * @param   $parentPics 父母拍摄
     * @return  boolean
     */
    function addPicture($activityId, $userId, $childPics, $parentPics){
        foreach($childPics as $val){
            $batchItem[]    = [
                'activity_id'   => $activityId,
                'user_id'   => $userId,
                'path'  => $val['path'],
                'beautify_path' => $val['beautify_path'],
                'origin'    => 2,
                'create_time'   => time(),
                'update_time'   => time(),
                'status'    => 1
            ];
        }

        foreach($parentPics as $val){
            $batchItem[]    = [
                'activity_id'   => $activityId,
                'user_id'   => $userId,
                'path'  => $val['path'],
                'beautify_path' => $val['beautify_path'],
                'origin'    => 1,
                'create_time'   => time(),
                'update_time'   => time(),
                'status'    => 1
            ];
        }

        if(!$batchItem){
            $this->errMsg   = '数据为空!';
            return false;
        }

        $result     = M('t_activity_picture', null)->addAll($batchItem);
        if($result === false){
            $this->errMsg   = '添加失败!';
            return false;
        }
        return true;
    }

    /**
     * 获取主题相册
     * @param   $userId
     * @param   $activityId
     * @return  $list or false
     */
    function getPictureList($userId, $activityId){
        $list   = M('t_activity_picture', null)
            ->where('user_id = %d and activity_id = %d', $userId, $activityId)
            ->order('id desc')
            ->cache(true)
            ->select();

        $list   = $this->_analyField('activity_picture', $list, true);
        
        return $list;
    }
}